// pages/picShow/picShow.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
   id:'',//写真id
   photo_url:'',
   is_like:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true
    })
    this.setData({
      id:options.id
    })
    if (options.scene == 1044) {
      wx.getShareInfo({
        shareTicket: opt.shareTicket,
        success: function (res) {
          var encryptedData = res.encryptedData;
          var iv = res.iv;
        }
      })
    }
    this.init()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '我在法斗家的写真，快来看看吧！',
        path: '/pages/picShow/picShow?id=' + this.data.id,
        success: function (res) {
          // 转发成功
          var shareTickets = res.shareTickets;
          if (shareTickets.length == 0) {
            return false;
          }
          wx.getShareInfo({
            shareTicket: shareTickets[0],
            success: function (res) {
              var encryptedData = res.encryptedData;
              var iv = res.iv;
            }
          })
        },
        fail: function (res) {
          // 转发失败
          wx.showToast({
            icon:'none',
            title: '转发失败',
          })
        }

      }
    }
  },
  init(){
    this.setData({
      is_like:this.data.is_like == 0 ? 1 :0
    })
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Photo/detail',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'id': this.data.id,
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            wx.hideLoading();
            if (res.data.success == '1') {
              this.setData({
                photo_url:res.data.data.photo_url,
                is_like:res.data.data.is_like
              })
            }
          }
        })
      }
    })
  },
  islike(){
    //点击喜欢该写真
      
      wx.request({
        url: base_url + '/Photo/likeOrCancel',
        data: {
          'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
          'id': this.data.id,
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          if (res.data.success == '1') {
            this.setData({
              is_like : !this.data.is_like
            })

          } else {
            wx.showToast({
              title: '服务器出错啦',
              mask: true,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    
  }
})